package com.network.utils;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.schedulers.Schedulers;


public class RxUtil {

    private final static ObservableTransformer schedulersTransformerIO = new ObservableTransformer() {
        @Override
        public ObservableSource apply(@NonNull Observable observable) {
            return observable.subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };

    private final static ObservableTransformer schedulersTransformerComputation = new ObservableTransformer() {
        @Override
        public ObservableSource apply(@NonNull Observable observable) {
            return observable.subscribeOn(Schedulers.computation()).unsubscribeOn(Schedulers.computation()).observeOn(AndroidSchedulers.mainThread());
        }
    };


    /**
     * 统一线程处理
     *
     * @return
     */
    private static FlowableTransformer rxSchedulerHelper = new FlowableTransformer() {
        @Override
        public Flowable apply(Flowable observable) {
            return observable.subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
        }
    };

    public static FlowableTransformer getRxSchedulerHelper() {
        return rxSchedulerHelper;
    }


    public static ObservableTransformer applySchedulersIO() {
        return schedulersTransformerIO;
    }

    public static ObservableTransformer applySchedulersComputation() {
        return schedulersTransformerComputation;
    }



}